package com.ym.soso.service.search.service;

import com.ym.base.domain.MobileSearchParam;
import com.ym.core.es.query.QueryParser;
import com.ym.core.es.service.AbstractHeightWordOperationService;
import com.ym.soso.domain.entity.AppHealthPlateEntity;
import com.ym.soso.enums.PublishStatusEnum;
import com.ym.soso.service.IAppHealthPlateSV;
import com.ym.soso.service.search.index.AppHealthPlateIndex;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: 李涛
 * @version: 2020年01月07日 14:17
 */
@Service
@Slf4j
public class AppHealthIndexService extends AbstractHeightWordOperationService<AppHealthPlateIndex, AppHealthPlateEntity> {

	@Autowired
	private IAppHealthPlateSV appHealthPlateSV;

	@Override
	public List<AppHealthPlateEntity> getData() {
		List<AppHealthPlateEntity> initData = appHealthPlateSV.selectAll();
		return initData;
	}

	/**
	 * 数据库实体转换为luce实体
	 *
	 * @param data
	 * @return
	 */
	@Override
	public AppHealthPlateIndex beanCovert(AppHealthPlateEntity data) {
		AppHealthPlateIndex build = new AppHealthPlateIndex();
		build.setTitle(data.getTitle());
		build.setId(data.getId().toString());
		build.setPublishStatus(data.getPublishStatus());
		return build;
	}


	@Override
	protected QueryParser buildSearchIdsConditions(MobileSearchParam args) {
		String keyword = args.getKeyword();
		// 构建查询语句
		QueryParser queryParser = new QueryParser(dataTypeClass);
		BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
		boolQueryBuilder.filter(QueryBuilders.termQuery("publishStatus", PublishStatusEnum.HAS_BEEN_RELEASED.getKey()));
		MultiMatchQueryBuilder mulQuery = QueryBuilders.multiMatchQuery(keyword, "title");
		mulQuery.minimumShouldMatch("25%");
		boolQueryBuilder.must(mulQuery);
		queryParser.query(boolQueryBuilder, new String[]{"id"});
		return queryParser;
	}

}
